﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARUserControls
{
    /// <summary>
    /// Interaction logic for SAR_DropDownListBox.xaml
    /// </summary>
    public partial class SAR_DropDownListBox : UserControl
    {
        public SAR_DropDownListBox()
        {
            InitializeComponent();
            this.comboBoxField.DropDownOpened += new EventHandler(comboBoxField_DropDownOpened);
            this.comboBoxField.DropDownClosed += new EventHandler(comboBoxField_DropDownClosed);
            this.comboBoxField.SelectionChanged +=new System.Windows.Controls.SelectionChangedEventHandler(comboBoxField_SelectionChanged);
            
        }

        //定义一个事件
        public event SelectionChangedEventHandler SARSelectionChanged = null;


        public delegate void SelectionChangedEventHandler(int selectedIndex);

        private void comboBoxField_SelectionChanged(object sender, EventArgs e)
        {
            if (SARSelectionChanged != null)
            {
                SARSelectionChanged.Invoke(this.comboBoxField.SelectedIndex);
            }
        }

        private void comboBoxField_DropDownClosed(object sender, EventArgs e)
        {
            this.mouseCapture.Opacity = 0;            
        }

        void comboBoxField_DropDownOpened(object sender, EventArgs e)
        {
            this.mouseCapture.Opacity = 0.4;
        }

        private void mouseCapture_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.comboBoxField.IsDropDownOpen = !this.comboBoxField.IsDropDownOpen;
        }

        private void comboBoxField_SelectionChanged(object sender, MouseButtonEventArgs e)
        {

        }

        public ComboBox SARComboBox
        {
            get
            {
                return this.comboBoxField;
            }
            
        }

        public ItemCollection SARItems
        {
            get
            {
                return this.comboBoxField.Items;
            }
        }

        private string errorMessage = "";

        /// <summary>
        /// 错误消息，代替ErrorProvider的功能，如果设置""即可消除
        /// </summary>
        public string SARErrorMessage
        {
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    errorMessage = "";
                    imgError.Visibility = Visibility.Hidden;
                }
                else
                {
                    errorMessage = value;
                    imgError.Visibility = Visibility.Visible;
                    imgError.ToolTip = value;
                }
            }
            get
            {
                return errorMessage;
            }
        }

        private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                this.Opacity = 1;
            }
            else
            {
                this.Opacity = 0.5;
            }
        }
    }
}
